--Target Bar
local Head = MClass("Head")

--ctor 初始化UI init设置静态数据 setData设置动态数据
function Head:ctor()
    --self.root 默认为根节点
    self.classIcon = self:findChild("class_icon")
    self.icon = self:findChild("icon_img")
    self.teamBg = self:findChild("bg_team")
    self.enemyBg = self:findChild("bg_foe")
    self.enemyBgBoss = self:findChild("bg_foe_1")
    self.zhezhao = self:findChild("03Zhezhao")
    self.zhezhao:setVisible(false)
    local zorder = self.icon:getLocalZOrder()
    --self:addChild(self:CreateClippingNode(self.zhezhao,self.icon),zorder)

	self:init()
end

function Head:CreateClippingNode(stencilNode,contentNode)
    if tolua.isnull(stencilNode) or tolua.isnull(contentNode) then
        print("getOrCreateHeroIconClipping fail")
    else
        print("getOrCreateHeroIconClipping ok")
        contentNode:retain()
        if contentNode:getParent() then
        	contentNode:removeFromParent()
        end
        stencilNode:retain()
        if stencilNode:getParent() then
        	stencilNode:removeFromParent()
        end
        local clipNode = cc.ClippingNode:create()
        clipNode:setInverted(false)
        clipNode:setAlphaThreshold(0.5)
        if stencilNode then
            clipNode:setStencil(stencilNode)
            stencilNode:setVisible(true)
            contentNode:setContentSize(stencilNode:getContentSize())
            contentNode:setPosition(stencilNode:getPosition())
        end
        clipNode:addChild(contentNode)
        contentNode:release()
        stencilNode:release()
      	return clipNode
    end
end

function Head:init()
   
end

function Head:setData(view,entityModel)
	if view:getClassIcon() then
        self.classIcon:setVisible(true)
		self.classIcon:loadTexture(view:getClassIcon())
    else
        if self.classIcon then
            self.classIcon:setVisible(false)
        end
	end

    if view:isEnemy() then
        if  self.teamBg then
            self.teamBg:setVisible(false)
        end

        if self.enemyBg then
            if entityModel:isElite() then
                self.enemyBg:setVisible(true)
                if self.enemyBgBoss then
                    self.enemyBgBoss:setVisible(false)
                end
            elseif entityModel:isBoss() then
                self.enemyBg:setVisible(false)
                if self.enemyBgBoss then
                    self.enemyBgBoss:setVisible(true)
                end
            else
                self.enemyBg:setVisible(true)
            end
            
        end
    else
        if self.teamBg then
            self.teamBg:setVisible(true)
        end
        if self.enemyBg then
            self.enemyBg:setVisible(false)
        end
    end

    local icon = view:getCurrentIcon()
	if icon then
		self.icon:loadTexture(icon)
    else
        entityModel:getFaceSideIcon(function(path)
            if self and cc.isAlive(self.icon) then
                self.icon:loadTexture(path)
            end
        end)
	end
	
	--dump(data)
end

return Head